package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.common.RetEnum;
import com.ltxtiyu.video.entity.Manager;
import com.ltxtiyu.video.service.ms.ManagerService;
import com.ltxtiyu.video.tool.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("loginController")
@Controller
public class LoginController {
    @Reference
    private ManagerService ms;

    /**
     * 登陆
     * @param session
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String login(HttpSession session, String username, String password){

        Map<String ,Object> param=new HashMap<>();
        param.put("userName",username);
        param.put("password", SecurityUtil.encrypt(password));
        Manager b = ms.checkUsernameAndPassword(param);
        if(b != null){
            session.setAttribute("loginUser",b);
            return JsonTool.genJson(RetEnum.SUCCESS);
        }else{
            return JsonTool.genJson(RetEnum.OAUTH_LOGIN_FAIL);
        }


    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public  String  logout(HttpSession session){
        return "login";
    }
}
